from functools import reduce

def add(x,y):
    y = f'{y}=VALUES({y}),'
    return x+y

def insert_sql(table_name,fields,datas):
    in_fields = reduce(lambda x,y:x +','+y, fields)
    c = f'INSERT INTO {table_name}({in_fields}) VALUES' + str(
            datas).strip('[]')
    s = reduce(add,fields)
    a = c + ' ON DUPLICATE KEY UPDATE ' + s.replace(fields[0],f'{fields[0]}=VALUES({fields[0]}),').strip(',')
    return a

if __name__ == '__main__':
    datas = [('a', 'b', 'c', 'd'), ('q', 'w', 'e', 'r')]
    # 使用时直接调用insert_sql即可，三个参数，一：表名，二：插入的字段数，三：插入的数据（列表中放数组形式）
    # 调用举例
    sql = insert_sql('website100_parse_fields',('url','is_ok','text','facebook'),datas)
    print(sql)